﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;
using System.Data.SqlClient;
using System.Data;

namespace xTeam.Hengle.DataAccess
{
    public class ClassifyDataAccess
    {
        public ClassifyDataAccess(string connectionString)
        {
            SQLProcedureHelper.ConnectionString = connectionString;
        }

        public int Save(EClassify classify)
        {
            int result = 0;
            SqlParameter[] para = 
            {
                 new SqlParameter() { ParameterName = "@ClassifyId", SqlDbType = SqlDbType.Int, Value = classify.ClassifyId },
                 new SqlParameter() { ParameterName = "@Title", SqlDbType = SqlDbType.NVarChar, Size = 50, Value = classify.Title },
                 new SqlParameter() { ParameterName = "@KeyWords", SqlDbType = SqlDbType.NVarChar, Size = 500, Value = classify.KeyWords },
                 new SqlParameter() { ParameterName = "@Description", SqlDbType = SqlDbType.NVarChar, Size = 500, Value = classify.Description },
                 new SqlParameter() { ParameterName = "@ParentId", SqlDbType = SqlDbType.Int, IsNullable = true, Value = classify.ParentId },
                 new SqlParameter() { ParameterName = "@Sequence", SqlDbType = SqlDbType.Int, Value = classify.Sequence },
                 new SqlParameter() { ParameterName = "@ModuleKey", SqlDbType = SqlDbType.NVarChar, Size = 50, Value = classify.ModuleKey },
                 new SqlParameter() { ParameterName = "@TemplateName", SqlDbType = SqlDbType.NVarChar, Size = 50, Value = classify.TemplateName }
            };
            result = SQLProcedureHelper.ExecuteNonQuery("classify_update", para);

            return result;
        }

        public int Delete(int classifyId)
        {
            int result = 0;
            SqlParameter[] para = { new SqlParameter("@ClassifyId", SqlDbType.NVarChar) { Value = classifyId } };
            result = SQLProcedureHelper.ExecuteNonQuery("classify_delete", para);
            return result;
        }

        public EClassify GetClassifyById(int classifyId)
        {
            EClassify classify = null;

            SqlParameter[] para = { new SqlParameter("@ClassifyId", SqlDbType.NVarChar) { Value = classifyId } };
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("classify_select", para);

            if (reader.HasRows)
            {
                classify = new EClassify();
                reader.Read();
                classify.ClassifyId = Convert.ToInt32(reader["ClassIfyId"]);
                classify.Title = reader["Title"].ToString();
                classify.Description = reader["Description"].ToString();
                classify.KeyWords = reader["KeyWords"].ToString();
                classify.ModuleKey = reader["ModuleKey"].ToString();
                classify.ParentId = Convert.ToInt32(reader["ParentId"]);
                classify.Sequence = Convert.ToInt32(reader["Sequence"]);
                classify.TemplateName = reader["TemplateName"].ToString();
            }

            return classify;
        }

        public List<EClassify> GetClassLisytByModuleKey(string moduleKey, int flag)
        {
            List<EClassify> classifyList = new List<EClassify>();
            SqlParameter[] para = { new SqlParameter("@ModuleKey", SqlDbType.NVarChar) { Value = moduleKey },
                                  new SqlParameter("@Flag", SqlDbType.Int) { Value = flag }};
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("classify_selectList", para);
            while (reader.Read())
            {
                classifyList.Add(new EClassify()
                {
                    ClassifyId = Convert.ToInt32(reader["ClassIfyId"]),
                    Title = reader["Title"].ToString(),
                    KeyWords = reader["KeyWords"].ToString(),
                    Description = reader["Description"].ToString(),
                    ParentId = Convert.ToInt32(reader["ParentId"]),
                    Path = reader["Path"].ToString(),
                    Sequence = Convert.ToInt32(reader["Sequence"]),
                    Floor = Convert.ToInt32(reader["Floor"]),
                    ModuleKey = reader["ModuleKey"].ToString(),
                    TemplateName = reader["TemplateName"].ToString()
                });
            }
            reader.Close();
            return classifyList;
        }


        public List<EClassify> GetHengLeMenu()
        {
            List<EClassify> classifyList = new List<EClassify>();
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("HengLe_menu");
            while (reader.Read())
            {
                classifyList.Add(new EClassify()
                {
                    ClassifyId = Convert.ToInt32(reader["ClassIfyId"]),
                    Title = reader["Title"].ToString(),
                    KeyWords = reader["KeyWords"].ToString(),
                    Description = reader["Description"].ToString(),
                    ParentId = Convert.ToInt32(reader["ParentId"]),
                    Path = reader["Path"].ToString(),
                    Sequence = Convert.ToInt32(reader["Sequence"]),
                    Floor = Convert.ToInt32(reader["Floor"]),
                    ModuleKey = reader["ModuleKey"].ToString()
                });
            }
            reader.Close();
            return classifyList;
        }
    }
}
